//请求方式的封装
import axios from 'axios';
import { Message } from 'element-ui';
// axios.defaults.timeout = 5000 * 60;
// axios.defaults.timeout = 5000;
axios.defaults.baseURL = process.env.BASE_SERVER_URL;

//http request 拦截器
axios.interceptors.request.use(
  config => {
    const token = sessionStorage.getItem("token");
    if (token) {
      config.headers.common['Authorization'] = token
    }
    return config;
  },
  error => {
    router.push('/login')
    return Promise.reject(err);
  }
);
let params = new URLSearchParams();
let formData = new FormData();

axios.interceptors.response.use(
  response => {
    if (response.data.errCode == 2) {
      router.push({
        path: "/login",
        //从哪个页面跳转
      })
    }
    return response;
  },
  error => {
    return Promise.reject(error)
  }
)
/** 
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function fetch (url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err)
      })
  })
}
export function fetch2 (url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params,
      responseType: 'blob'
    })
      .then(response => {
        resolve(response);
      })
      .catch(err => {
        reject(err)
      })
  })
}
export function fetch3 (url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params,
      responseType: 'arraybuffer'
    })
      .then(response => {
        let val = 'data:image/png;base64,' + btoa(new Uint8Array(response.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))
        resolve(val);
      })
      .catch(err => {
        reject(err)
      })
  })
}

export function post2 (url, par, config) {
  return new Promise((resolve, reject) => {
    axios.post(url, par, config)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post (url, params) {
  return new Promise((resolve, reject) => {
    let config = {
      headers: { 'Content-Type': 'application/json' }
    };
    axios.post(url, params, config)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}
/**
 * 封装上传upload请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function upload (url, formData) {
  return new Promise((resolve, reject) => {
    let config = {
      headers: { 'Content-Type': 'multipart/form-data' }
    };
    axios.post(url, formData, config)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}
export function upload2 (url, formData, formData2, config) {
  return new Promise((resolve, reject) => {
    axios.post(url, formData, formData2, config)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}
/**
* 封装patch请求
* @param url
* @param data
* @returns {Promise}
*/
export function patch (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}
/**
* 封装put请求
* @param url
* @param data
* @returns {Promise}
*   div
*/
export function put (url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}
